<?php

namespace app\admin\controller;


use app\admin\model\Column;
use app\admin\model\ColumnPage;
use app\admin\model\Config;
use app\admin\model\Substation;
use app\admin\model\System;

/**
 * 网站设置
 * Class Systems
 * @package app\admin\controller
 */
class Systems extends Base
{

    //分站列表
    public function substationList(){
        if(input("?table")) {
            $keyword = input("keyword/s");
            $Substation = new Substation();
            $result["code"] = 0;
            $result['data'] = $Substation->substationList($keyword);
            $result['count'] = $Substation->substationListCount($keyword);
        }
        return !empty($result) ? json($result) : view('substationList',[
            'title'         =>  '分站列表',
        ]);
    }

    //添加修改分站
    public function substationAdd(){
        $substation_id = input("substation_id/d",0);
        $Substation = new Substation();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $Substation->checkData($data, [
                ["substation_name", "require", "分站名称不能为空"],
                ["substation_domain", "require", "分站域名不能为空"],
            ]);
            if ($msg === true) {
                if($Substation->checkFieldOnly("substation_name",$data["substation_name"],"substation_id",$substation_id)){
                    $result["code"] = 1;
                    $result["msg"] = '分站名称已存在';
                }else{
                    if($Substation->checkFieldOnly("substation_domain",$data["substation_domain"],"substation_id",$substation_id)){
                        $result["code"] = 1;
                        $result["msg"] = '分站名域名已存在';
                    }else{
                        if($Substation->mySave($data,"substation_id", $substation_id)){
                            $result["code"] = 0;
                            $result["msg"] = "操作成功！";
                        }else{
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
        }else {
            if (!empty($substation_id)) {
                //修改获取详情
                $dataInfo = $Substation->dataInfo("substation_id",$substation_id);
                $this->assign('dataInfo', $dataInfo);
            }
        }
        return !empty($result) ? json($result): view('substationAdd',[
            'title'         =>  '添加修改分站',
        ]);
    }

    //删除分站
    public function substationDel(){
        $substation_id = input("substation_id/d",0);
        $Substation = new Substation();
        if(!empty($substation_id)){
            if($Substation->del("substation_id",$substation_id,true)){
                $result["code"] = 0;
                $result["msg"] = "操作成功!";
            }else{
                $result["code"] = 10000;
                $result["msg"] = "操作失败！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "substation_id，参数为空!";
        }
        return json($result);
    }

    //图纸上传设置
    public function drawingUploadSystem(){
        $System = new System();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $System->checkData($data, [
                ["center", "require", "请上传居中水印图"],
                ["right", "require", "请上传右下角水印图"],
            ]);
            if ($msg === true) {
                $data["switch"] = !empty($data["switch"]) ? $data["switch"] : 0;
                if($System->drawingUploadSystemSave($data)){
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                }else{
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败！";
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
        }else{
            $dataInfo = $System->drawingUploadSystemInfo();
            $this->assign('dataInfo', $dataInfo);
        }
        return !empty($result) ? json($result): view('drawingUploadSystem',[
            'title'         =>  '图纸上传设置',
        ]);
    }

    //栏目设置
    public function columnSetup(){
        if(input("?table")) {
            $keyword = input("keyword/s");
            $Column = new Column();
            $result["code"] = 0;
            $result['data'] = $Column->columnList($keyword);
            $result['count'] = $Column->columnListCount($keyword);
        }
        return !empty($result) ? json($result): view('columnSetup',[
            'title'         =>  '栏目设置',
        ]);
    }

    //添加编辑栏目
    public function columnAdd(){
        $column_id = input("column_id/d",0);
        $Column = new Column();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $Column->checkData($data, [
                ["column_name", "require", "栏目名称不能为空"],
                ["column_url", "require", "分站链接不能为空"],
                ["substation_id", "require", "请选择发布网站"],
            ]);
            if ($msg === true) {
                if($Column->checkPKFieldOnly("column_name",$data["column_name"],$column_id,$data["substation_id"])){
                    $result["code"] = 1;
                    $result["msg"] = '栏目名称已存在';
                }else{
                    if($Column->checkSave($column_id,$data["substation_id"])) {
                        if ($Column->mySave($data, "column_id", $column_id)) {
                            $result["code"] = 0;
                            $result["msg"] = "操作成功！";
                        } else {
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }else{
                        $result["code"] = 20000;
                        $result["msg"] = "没有权限操作！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
        }else {
            if (!empty($column_id)) {
                //修改获取详情
                $dataInfo = $Column->PkInfo($column_id);
                $this->assign('dataInfo', $dataInfo);
            }
            $Substation = new Substation();//分站类
            $userSubstationList = $Substation->userList();//获取分站集合
            $this->assign('userSubstationList',$userSubstationList);
        }
        return !empty($result) ? json($result): view('columnAdd',[
            'title'         =>  '添加编辑栏目',
            'RadioIs'   =>  config("IsNoRadio"),//获取Radio配置
        ]);
    }

    //删除栏目
    public function columnDel(){
        $column_id = input("column_id/d",0);
        $Column = new Column();
        if(!empty($column_id)){
            if($Column->checkSave($column_id,0)) {
                if ($Column->PKDel($column_id, true)) {
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败！";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操作！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "column_id，参数为空!";
        }
        return json($result);
    }

    //单页栏目设置
    public function columnPageSetup(){
        if(input("?table")) {
            $keyword = input("keyword/s");
            $ColumnPage = new ColumnPage();
            $result["code"] = 0;
            $result['data'] = $ColumnPage->columnList($keyword);
            $result["data"] = collection($result["data"])->toArray();
            if(empty($keyword)) {//搜索不保持树结构
                $result["data"] = Menus::arrayToTree($result["data"],0,0,"page_id");
            }
        }
        return !empty($result) ? json($result): view('columnPageSetup',[
            'title'         =>  '单页栏目设置',
        ]);
    }

    //添加编辑单页栏目
    public function columnPageAdd(){
        $page_id = input("page_id/d",0);
        $ColumnPage = new ColumnPage();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $ColumnPage->checkData($data, [
                ["page_name", "require", "栏目名称不能为空"],
                ["sort", "require", "排序不能为空"],
                ["text_content", "require", "正文内容不能为空"],
                ["substation_id", "require", "请选择发布网站"],
            ]);
            if ($msg === true) {
                if($ColumnPage->checkPKFieldOnly("page_name",$data["page_name"],$page_id,$data["substation_id"])){
                    $result["code"] = 1;
                    $result["msg"] = '栏目名称已存在';
                }else{
                    if($ColumnPage->checkParentId($data["parent_id"],$data["substation_id"])) {
                        if ($ColumnPage->checkSave($page_id, $data["substation_id"])) {
                            if ($ColumnPage->mySave($data, "page_id", $page_id)) {
                                $result["code"] = 0;
                                $result["msg"] = "操作成功！";
                            } else {
                                $result["code"] = 10000;
                                $result["msg"] = "操作失败！";
                            }
                        } else {
                            $result["code"] = 20000;
                            $result["msg"] = "没有权限操作！";
                        }
                    }else{
                        $result["code"] = 30000;
                        $result["msg"] = "对应站点没有该上级分类！";
                    }
                }
            }else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
        }else {
            if (!empty($page_id)) {
                //修改获取详情
                $dataInfo = $ColumnPage->PkInfo($page_id);
                $this->assign('dataInfo', $dataInfo);
            }
            $columnList = Menus::arrayToTree(collection($ColumnPage->columnList())->toArray(),0,0,"page_id");
            $this->assign('columnList',$columnList);
            $Substation = new Substation();//分站类
            $userSubstationList = $Substation->userList();//获取分站集合
            $this->assign('userSubstationList',$userSubstationList);
        }
        return !empty($result) ? json($result): view('columnPageAdd',[
            'title'         =>  '添加编辑单页栏目',
            'ueditor'                   =>  true,//开启ue编辑器
        ]);
    }

    //删除单页栏目
    public function columnPageDel(){
        $page_id = input("page_id/d",0);
        $ColumnPage = new ColumnPage();
        if(!empty($page_id)){
            $parent_count = $ColumnPage->getFieldCount("parent_id",$page_id);
            if(!$parent_count) {
                if ($ColumnPage->checkSave($page_id, 0)) {
                    if ($ColumnPage->PKDel($page_id, true)) {
                        $result["code"] = 0;
                        $result["msg"] = "操作成功!";
                    } else {
                        $result["code"] = 10000;
                        $result["msg"] = "操作失败！";
                    }
                } else {
                    $result["code"] = 20000;
                    $result["msg"] = "没有权限操作！";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "该栏目下有（".$parent_count."）个子集栏目！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "page_id，参数为空!";
        }
        return json($result);
    }

    //细节设置
    public function detailSetting(){
        $System = new System();
        $Config = new Config();
        if(request()->isPost()){
            $data = input("post.");
            $data["index_style"] = !empty($data["index_style"]) ? explode(",",$data["index_style"]) : [];
            if($System->detailSettingSave($data)){
                $result["code"] = 0;
                $result["msg"] = "操作成功!";
            }else{
                $result["code"] = 10000;
                $result["msg"] = "操作失败！";
            }
        }else{
            $dataInfo = $System->detailSettingInfo();
            $this->assign('dataInfo', $dataInfo);
        }
        return !empty($result) ? json($result): view('detailSetting',[
            'title'         =>  '细节设置',
            'DrawingStyle'  => $Config->getConfigTypeList("DrawingStyle"),
        ]);
    }
}
